<template>
  <div class="content">
    <div class="content-layout">
      <div class="content-layout-left">
        <div class="layout-item">
          <span class="span-btn">>></span>
        </div>
        <div class="layout-item">
          <el-button type="primary" plain @click="handleAdd">新建</el-button>
        </div>
      </div>
    </div>
    <div class="content-body">
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column
          prop="docId"
          label="文件编号"
          min-width="60"
          show-overflow-tooltip
        />
        <el-table-column
          prop="docName"
          label="文件名称"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          prop="uploadTime"
          label="上传时间"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          prop="operator"
          label="操作人"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          prop="remark"
          label="备注"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="120">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click.prevent="handleUpdate(scope.row)"
            >
              修改
            </el-button>
            <el-button
              link
              type="primary"
              size="small"
              @click.prevent="handleDel(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        layout="total, sizes, prev, pager, next"
        @pagination="getData"
      />
    </div>

    <DataArchiveEdit
      ref="archiveEditRef"
      :is-add="isAdd"
      @refresh="getData"
    ></DataArchiveEdit>
  </div>
</template>

<script setup>
import { reactive, ref, toRefs, onMounted } from "vue";
import { listSource, delSource } from "@/api/system/datasource.js";
import DataArchiveEdit from "./dataArchiveEdit.vue";
import { ElMessage, ElMessageBox } from "element-plus";
// 查询参数
let queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
});
// 列表数据
let tableData = ref([]);
// 分页总数
let total = ref(0);
let loading = ref(true);
const archiveEditRef = ref(null);
// 新增或修改按钮点击
let isAdd = ref(false);
onMounted(() => {
  getData();
});
/** 获取表格数据 */
const getData = () => {
  loading = true;
  const temp = {
    docId: 0,
    docName: "文档名称",
    uploadTime: "2023-06",
    operator: "管理员",
    remark: "备注",
  };
  for (let i = 0; i < 9; i++) {
    let item = { ...temp };
    item.docId = i;
    item.docName += i;
    item.remark += i;
    tableData.value.push(item);
  }
  total.value = tableData.value.length;
  loading = false;
  // listSource(queryParams).then((res) => {
  //   tableData.value = [...res.rows];
  //   total.value = res.total;
  //   loading = false;
  // });
};
/** 新增 */
const handleAdd = () => {
  isAdd.value = true;
  archiveEditRef.value.open = true;
};
/** 修改 */
const handleUpdate = (row) => {
  isAdd.value = false;
  Object.assign(archiveEditRef.value.form, row);
  archiveEditRef.value.open = true;
};
/** 删除 */
const handleDel = (row) => {
  ElMessageBox.confirm("确定删除编号为" + row.docId + "的数据源？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "Warning",
  }).then(() => {
    // delSource(row.id).then((res) => {
    //   ElMessage({
    //     type: "success",
    //     message: "删除成功！",
    //   });
    //   getData();
    // });
  });
};
</script>
